package com.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/json")
public class JsonController {

    @RequestMapping("/jsonPage")
    public String intoJsonPage(){
        return "handlerJson";
    }

    @RequestMapping("/getPojoJson")
    public String getPojoJsonData(@RequestBody Student student){

        System.out.println(student);

        return "handlerJson";
    }

    @RequestMapping("/getStringJson")
    public String getStringJsonData(@RequestBody String json) throws JsonProcessingException {

        System.out.println(json);
        ObjectMapper mapper = new ObjectMapper();

        //把JSON格式的字符串处理成POJO类型
        Student student = mapper.readValue(json,Student.class);
        System.out.println(student);

        //把JSON格式的字符串处理成Map类型
        Map<String ,Object> dataMap = mapper.readValue(json, new TypeReference<Map<String ,Object>>() {});
        System.out.println(dataMap);
        System.out.println(dataMap.get("stuName"));
        System.out.println(dataMap.get("age"));


        return "handlerJson";
    }


    //从Controller向页面返回数据，需要在方法上面添加 @ResponseBody 注解
    @RequestMapping("/sendPojoJson")
    @ResponseBody
    public Student sendPojoJsonData(int id){

        Student student;
        if(id==1){
            student = new Student("tom",1);
        }else{
            student = new Student("other",10);
        }

        return student;
    }

    @RequestMapping("/getSingleJson")
    @ResponseBody
    public String sendSingleJsonData() throws JsonProcessingException {

        Student student = new Student("tom", 100);

        //把POJO类型的对象转成JSON字符串形式
        ObjectMapper mapper = new ObjectMapper();
        String studentJsonString = mapper.writeValueAsString(student);

        System.out.println("student:"+studentJsonString);

        return studentJsonString;
    }

    @RequestMapping("/getMoreJson")
    @ResponseBody
    public List<Student> sendMoreJsonData() throws JsonProcessingException {

//        List<String> studentList = new ArrayList<>();
//
//        //把POJO类型的对象转成JSON字符串形式
//        ObjectMapper mapper = new ObjectMapper();
//
//        studentList.add(mapper.writeValueAsString(new Student("tom",1)));
//        studentList.add(mapper.writeValueAsString(new Student("jerry",2)));
//        studentList.add(mapper.writeValueAsString(new Student("张添齐",3)));
//        studentList.add(mapper.writeValueAsString(new Student("赵圣骏",4)));
//        studentList.add(mapper.writeValueAsString(new Student("开祥鹏",5)));


        List<Student> studentList = new ArrayList<>();

        studentList.add(new Student("tom",1));
        studentList.add(new Student("jerry",2));
        studentList.add(new Student("kitty",3));


        return studentList;
    }

}
